/*
 * ComplexListCopy.cpp
 *
 *  Created on: Aug 15, 2012
 *      Author: jh238d
 */

#include "complexNode.h"
#include <iostream>
using namespace std;

void clone_list(complexNode *head){
	if(!head) return;
	complexNode *cur = head;
	while(cur!=NULL){
		complexNode *cloned = new complexNode();
		cloned->data=cur->data;
		cloned->next = cur->next;
		cloned->sibling = NULL;

		cur->next = cloned;
		cur=cloned->next;
	}
}

void connect_sibling_nodes(complexNode *head){
	if(!head) return;
	complexNode *cur = head;
	while(cur!=NULL){
		cur->next->sibling = cur->sibling->next;
		cur=cur->next->next;
	}
}

complexNode* reconnect_nodes(complexNode *head){
	if(!head) return NULL;
	complexNode *cur = head;
	complexNode *result = head->next;
	while(cur!=NULL){
		complexNode *tmp = cur->next;
		cur->next = tmp->next;
		tmp->next = tmp->next->next;
		cur=cur->next;
	}
	return result;
}

complexNode* complex_list_copy(complexNode *head){
	clone_list(head);
	connect_sibling_nodes(head);
	return reconnect_nodes(head);
}
